package com.atguigu.ssyx.common.config;

import com.atguigu.ssyx.enums.ActivityType;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;


//@Component
public class StringActivityType2Coverter implements Converter<String, ActivityType> {
    @Override
    public ActivityType convert(String source) {

        if (source == null || source.trim().isEmpty()) {
            throw new IllegalArgumentException("活动类型不能为空");
        }

        // 尝试按枚举名称转换（如果前端传的是"FULL_REDUCTION"）
        try {
            return ActivityType.valueOf(source.toUpperCase());
        } catch (IllegalArgumentException e) {
            // 如果按名称转换失败，尝试按code值转换
            try {
                int code = Integer.parseInt(source);
                for (ActivityType activityType : ActivityType.values()) {
                    if (activityType.getCode().equals(code)) {
                        return activityType;
                    }
                }
            } catch (NumberFormatException ex) {
                // 既不是有效名称也不是数字code
            }
        }

        throw new IllegalArgumentException("非法的活动类型代码: " + source);
    }


}
